package com.example.splash

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View

import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.transition.Visibility
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.RouterPath
import com.example.common.adapter.BannerAdapter
import com.example.common.base.BaseActivity
import com.example.common.entity.BannerEntity
import com.example.splash.databinding.ActivitySplashBinding
import com.gyf.immersionbar.ImmersionBar
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer

class SplashActivity : BaseActivity<ActivitySplashBinding>() {

    private lateinit var mImgAdapter: BannerAdapter
    private var bannerData = mutableListOf<BannerEntity>()
    private lateinit var mCountDownTimer: CountDownTimer
    override fun initViews() {
        //初始化沉寂式状态栏
        ImmersionBar.with(this).init()

        //判断是否是第一次启动
        var isFirstBoot = MMKV.defaultMMKV().decodeBool("isFirstBoot", true)
        if (isFirstBoot) {
            //是第一次启动,在重新赋值,变成不是第一次启动
            MMKV.defaultMMKV().encode("isFirstBoot", false)
            mDatabinding.splashOneBanner.visibility = View.VISIBLE
            //添加图片数据
            bannerData.add(BannerEntity(R.drawable.img1, ""))
            bannerData.add(BannerEntity(R.drawable.img2, ""))
            bannerData.add(BannerEntity(R.drawable.img3, ""))
            //给适配器重新赋值
            mImgAdapter = BannerAdapter(bannerData)
            //给Banner添加适配器
            mDatabinding.appBanner.setAdapter(mImgAdapter)
            //与当前的Activity的生命周期相关联
            mDatabinding.appBanner.addBannerLifecycleObserver(this)
            //添加轮播图的样式
            mDatabinding.appBanner.indicator = CircleIndicator(this)
            //设置切换过度的效果
            mDatabinding.appBanner.addPageTransformer(AlphaPageTransformer())
            //设置轮播图不自动切换
//            mDatabinding.appBanner.isAutoLoop(false)
            //设置切换事件监听
            mDatabinding.appBanner.addOnPageChangeListener(object : OnPageChangeListener {
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int,
                ) {

                }

                override fun onPageSelected(position: Int) {
                    if (position == bannerData.size - 1) {
                        mDatabinding.btnStart.visibility = View.VISIBLE

                    }
                    else {
                        mDatabinding.btnStart.visibility = View.INVISIBLE

                    }

                }

                override fun onPageScrollStateChanged(state: Int) {

                }

            })
            mDatabinding.btnStart.setOnClickListener {
                ARouter.getInstance().build(RouterPath.PATH_MAIN_NAV_PAGE).navigation()
                this@SplashActivity.finish()
            }
        }else{
            mDatabinding.splashTwoImg.visibility = View.VISIBLE
            mCountDownTimer = object: CountDownTimer(3000,1000) {
                override fun onTick(p: Long) {
                    mDatabinding.tvSkip.text = "${p/1000}秒之后跳过"
                }

                override fun onFinish() {
                    ARouter.getInstance().build(RouterPath.PATH_MAIN_NAV_PAGE).navigation()
                    this@SplashActivity.finish()
                }
            }.start()



        }


    }

    override fun getLayoutId(): Int = R.layout.activity_splash

}